layui.extend({treeTable: 'treeTable'})
layui.use(['form', 'admin', 'ax', 'table', 'laytpl', 'element', 'treeTable', 'laypage'], function () {
    var $ax = layui.ax;
    var admin = layui.admin;
    var table = layui.table;
    var form = layui.form;
    var laytpl = layui.laytpl;
    var element = layui.element;
    var treeTable = layui.treeTable;
    var laypage = layui.laypage;

    var Current = {
        tableId: "table"
    };
    $('body').removeClass('layui-hide');
    form.on('submit(btnSubmit)', function (data) {
        Current.initTreeTable(data.field);
        return false;
    });

    var insTable;
    // 渲染表格
    Current.initTreeTable = function (param) {
        insTable = treeTable.render({
            elem: '#' + Current.tableId,
            toolbar: false,
            page: true,
            tree: {
                iconIndex: 1
            },
            cols: [[
                {type: 'numbers'},
                // {type: 'checkbox'},
                {field: 'instanceId', title: '网关实例'},
                {field: 'name', title: '接口名'},
                {field: 'version', title: '版本号'},
                {field: 'serviceId', title: 'serviceId'},
                {field: 'maxTime', title: '最大耗时(ms)'},
                {field: 'minTime', title: '最小耗时(ms)'},
                {field: 'avgTime', title: '平均耗时(ms)'},
                {field: 'totalRequestCount', title: '总调用次数'},
                {field: 'successCount', title: '成功次数'},
                {field: 'errorCount', title: '失败次数'},
                {
                    field: 'unsolvedErrorCount', title: '未解决错误', templet: function (d) {
                        var unsolvedErrorCount = d.unsolvedErrorCount;
                        if (unsolvedErrorCount === 0) {
                            return unsolvedErrorCount;
                        }
                        return '<span style="color: #f56c6c;text-decoration: underline;cursor: pointer;" onclick="showErr(\'' + d.routeId + '\')">' + d.unsolvedErrorCount + '</span>'
                    }
                },
            ]],
            reqData: function (data, callback) {
                if (data) {
                    let ajax = new $ax(system.contextPath + '/api/monitornew.routeid.data.get');
                    ajax.setType('get');
                    ajax.set({routeId: data.routeId});
                    var result = ajax.start();
                    if (result.status) {
                        callback(result.data);
                    }
                } else {
                    let ajax = new $ax(system.contextPath + '/api/monitornew.data.page');
                    ajax.setType('get');
                    ajax.set({pageIndex: 1, pageSize: 10});
                    if (null != param) {
                        ajax.set(param);
                    }
                    var result = ajax.start();
                    if (result.status) {
                        callback(result.data.records);
                        //执行一个laypage实例
                        laypage.render({
                            elem: 'test1',
                            layout: ['prev', 'page', 'next', 'count', 'limit', 'skip'],
                            count: result.data.total,
                            jump: function (obj, first) {
                                //首次不执行
                                if (!first) {
                                    let ajax = new $ax(system.contextPath + '/api/monitornew.data.page');
                                    ajax.setType('get');
                                    ajax.set({pageIndex: obj.curr, pageSize: obj.limit});
                                    var result = ajax.start();
                                    if (result.status) {
                                        callback(result.data.records);
                                    }
                                }
                            }
                        });
                    }
                }
            },
            style: 'margin-top:0;'
        });
    }
    Current.initTreeTable(null);

    Current.initColumn = function () {
        return [[
            {type: 'numbers'},
            {field: 'errorMsg', sort: false, edit: 'text', width: '20%', title: '错误内容'},
            {field: 'instanceId', sort: false, width: '10%', title: '实例ID'},
            {field: 'count', sort: false, width: '10%', title: '报错次数'},
            {field: 'gmtCreate', sort: false, width: '10%', title: '报错时间'},
            {align: 'center', toolbar: '#tableErrBar', width: '10%', title: '操作'}
        ]];
    };

    var tableLayer;
    window.showErr = function (routeId) {

        var ins = table.render({
            elem: '#tableErr',
            url: system.contextPath + '/api/monitornew.error.page',
            where: {routeId: routeId},
            page: true,
            request: {
                pageName: 'pageIndex' //页码的参数名称，默认：page
                , limitName: 'pageSize' //每页数据量的参数名，默认：limit
            },
            height: "380",
            cellMinWidth: 100,
            cols: Current.initColumn(),
            parseData: function (res) { //res 即为原始返回的数据
                return {
                    "code": res.code, //解析接口状态
                    "msg": res.msg, //解析提示文本
                    "count": res.data.total, //解析数据长度
                    "data": res.data.records //解析数据列表
                };
            }
            , response: {
                statusCode: 200
            }
            , size: 'lg',
            done: function (res, curr, count) {
                count || $('.layui-table-view').find('.layui-table-header').css('overflow', 'visible');
                $('.layui-table-body').find('div.layui-table-cell').find('select').each(function () {
                    var $this = $(this);
                    var val = $this.data('value');
                    var text;
                    $(this).children().each(function () {
                        if (val == this.value) {
                            text = this.text;
                            return;
                        }
                    })
                    //更新layui 选择器输入框的值
                    $this.next().find('.layui-select-title').find('input').val(text);
                    //更新layui 选择器的值 以及设置其样式
                    $this.val(val).parents('div.layui-table-cell').css('overflow', 'visible');
                })
                // var html = document.getElementById('part0-template').innerHTML;
                var html = $('#part0-template').html();
                tableLayer = layer.open({
                    area: '63%',
                    type: 1,
                    title: '',
                    content: html,
                    // content: $('#part0-template'),
                    yes: function (index, layero) {
                        //do something
                        layer.close(index);
                    }
                });
                form.render(null, 'tableErr');
            }
        });

    }

    window.solve = function (id) {
        layer.confirm('确认标记为已解决吗?', function (index) {
            var ajax = new $ax(system.contextPath + '/api/monitornew.error.solve');
            ajax.set({errorId: id});
            var result = ajax.start();
            if (result.status) {
                layer.close(tableLayer);
                layer.close(index);
                table.reload('tableErr');
                insTable.refresh();
            } else {
                system.error("操作失败！");
            }
        });
    }

});
